package com.rename.wechat.weixin.checkurl;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.rename.wechat.model.AccessToken;
import com.rename.wechat.weixin.message.resp.Article;
import com.rename.wechat.weixin.message.resp.Image;
import com.rename.wechat.weixin.message.resp.ImageMessage;
import com.rename.wechat.weixin.message.resp.Music;
import com.rename.wechat.weixin.message.resp.MusicMessage;
import com.rename.wechat.weixin.message.resp.NewsMessage;
import com.rename.wechat.weixin.message.resp.TextMessage;
import com.rename.wechat.weixin.utils.MessageUtil;
import com.rename.wechat.weixin.utils.WeixinUtil;

public class CoreAction extends ActionSupport {

	private static final long serialVersionUID = -3527377212954480155L;
	
	// 微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数
	private String signature;

	// 时间戳
	private String timestamp;

	// 随机数
	private String nonce;

	// 随机字符串
	private String echostr;

	public String getSignature() {
		return signature;
	}

	public void setSignature(String signature) {
		this.signature = signature;
	}

	public String getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(String timestamp) {
		this.timestamp = timestamp;
	}

	public String getNonce() {
		return nonce;
	}

	public void setNonce(String nonce) {
		this.nonce = nonce;
	}

	public String getEchostr() {
		return echostr;
	}

	public void setEchostr(String echostr) {
		this.echostr = echostr;
	}

	@Override
	public String execute() throws Exception {
		
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpServletResponse resp = ServletActionContext.getResponse();
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		PrintWriter pw = resp.getWriter();
		
		// 验证请求是否来自微信服务器
		if(Signature.checkSignature(signature, timestamp, nonce)){
			// 判断是否为GET请求，此便确定是首次接入微信还是微信服务器进行信息推送
			String method = req.getMethod().toLowerCase();
			if("get".equals(method)){
				System.out.println("首次接入成功");
				// 首次接入
				pw.print(echostr);
			}else{
				// 信息推送
				// 获取消息并转为map
				Map<String, String> map = MessageUtil.xmlToMap(req);
				String fromUserName = map.get("FromUserName");
				String toUserName = map.get("ToUserName");
				String msgType = map.get("MsgType");
				System.out.println("MsgType===>" + msgType);
				System.out.println("Event=====>" + map.get("Event"));
				
				// 文本消息
				if(MessageUtil.REQ_MESSAGE_TYPE_TEXT.equals(msgType)){
					String content = map.get("Content");
					
					if("1".equals(content)){ // 回复文本消息
						TextMessage textMessage = new TextMessage();
						textMessage.setFromUserName(toUserName);
						textMessage.setToUserName(fromUserName);
						textMessage.setCreateTime((new Date()).getTime());
						textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
						textMessage.setContent("您发送的消息是：" + content);
						String respMessage = MessageUtil.textMessageToXml(textMessage);
						System.out.println(respMessage);
						pw.print(respMessage);
					}else if("2".equals(content)){ // 回复图片消息
						ImageMessage imageMessage = new ImageMessage();
						imageMessage.setFromUserName(toUserName);
						imageMessage.setToUserName(fromUserName);
						imageMessage.setCreateTime((new Date()).getTime());
						imageMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_IMAGE);
						Image image = new Image();
						image.setMediaId("H54qRHtaaUgbX5meZNbXqOWPbMEXgQmpFOkT6BpMx3NHdqPNXU9AvDd0eNGty0ty");
						imageMessage.setImage(image);
						String respMessage = MessageUtil.imageMessageToXml(imageMessage);
						pw.print(respMessage);
					}else if("3".equals(content)){ // 回复语音消息
						
					}else if("4".equals(content)){ // 回复视频消息
						
					}else if("5".equals(content)){ // 回复音乐消息
						MusicMessage musicMessage = new MusicMessage();
						musicMessage.setFromUserName(toUserName);
						musicMessage.setToUserName(fromUserName);
						musicMessage.setCreateTime((new Date()).getTime());
						musicMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_MUSIC);
						Music music = new Music();
						music.setTitle("我们的歌");
						music.setDescription("这是一首好听的歌");
						music.setMusicUrl("http://www.kuwo.cn/yinyue/271333/");
						music.setHQMusicUrl("http://www.kuwo.cn/yinyue/271333/");
						music.setThumbMediaId("H54qRHtaaUgbX5meZNbXqOWPbMEXgQmpFOkT6BpMx3NHdqPNXU9AvDd0eNGty0ty");
						musicMessage.setMusic(music);
						String respMessage = MessageUtil.musicMessageToXml(musicMessage);
						System.out.println(respMessage);
						pw.print(respMessage);
					}else if("6".equals(content)){ // 回复图文消息
						NewsMessage newsMessage = new NewsMessage();
						List<Article> articleList = new ArrayList<Article>();
						
						Article article = new Article();
						article.setTitle("慕课网介绍");
						article.setDescription("慕课网是垂直的互联网IT技能免费学习网站。以独家视频教程、在线编程工具、学习计划、问答社区为核心特色。在这里，你可以找到最好的互联网技术牛人，也可以通过免费的在线公开视频课程学习国内领先的互联网IT技术。慕课网课程涵盖前端开发、PHP、Html5、Android、iOS、Swift等IT前沿技术语言，包括基础课程、实用案例、高级分享三大类型，适合不同阶段的学习人群。");
						article.setPicUrl("http://zapper.tunnel.mobi/Weixin/image/imooc.jpg");
						article.setUrl("www.imooc.com");
						articleList.add(article);
						
						newsMessage.setToUserName(fromUserName);
						newsMessage.setFromUserName(toUserName);
						newsMessage.setCreateTime(new Date().getTime());
						newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
						newsMessage.setArticles(articleList);
						newsMessage.setArticleCount(articleList.size());
						
						String respMessage = MessageUtil.newsMessageToXml(newsMessage);
						System.out.println(respMessage);
						pw.print(respMessage);
					}
					
				}else if(MessageUtil.REQ_MESSAGE_TYPE_IMAGE.equals(msgType)){ // 图片消息
					String picUrl = map.get("PicUrl");
					System.out.println(picUrl);
				}else if(MessageUtil.REQ_MESSAGE_TYPE_VOICE.equals(msgType)){ // 语音消息
					System.out.println("语音消息");
				}else if(MessageUtil.REQ_MESSAGE_TYPE_VIDEO.equals(msgType)){ // 视频消息
					System.out.println("视频消息");
				}else if(MessageUtil.REQ_MESSAGE_TYPE_SHORTVIDEO.equals(msgType)){ // 小视频消息
					System.out.println("小视频消息");
				}else if(MessageUtil.REQ_MESSAGE_TYPE_LOCATION.equals(msgType)){ // 地理位置消息
					System.out.println("地理位置消息");
				}else if(MessageUtil.REQ_MESSAGE_TYPE_LINK.equals(msgType)){ // 链接消息
					System.out.println("链接消息");
				}else if(MessageUtil.REQ_MESSAGE_TYPE_EVENT.equals(msgType)){ // 事件推送
					String event = map.get("Event");
					
					if(MessageUtil.REQ_MESSAGE_EVENT_SUBSCRIBE.equals(event)){
						System.out.println("用户关注事件");
					}else if(MessageUtil.REQ_MESSAGE_EVENT_UNSUBSCRIBE.equals(event)){
						System.out.println("用户取消关注事件");
					}else if(MessageUtil.REQ_MESSAGE_EVENT_CLICK.equals(event)){
						System.out.println("点击事件");
					}else if(MessageUtil.REQ_MESSAGE_EVENT_VIEW.equals(event)){
						System.out.println("链接事件");
					}else if(MessageUtil.REQ_MESSAGE_EVENT_PIC_PHOTO_OR_ALBUM.equals(event)){
						System.out.println("弹出拍照或者相册发图的事件推送");
					}else if(MessageUtil.REQ_MESSAGE_EVENT_PIC_SYSPHOTO.equals(event)){
						System.out.println("弹出系统拍照发图的事件推送");
					}else if(MessageUtil.REQ_MESSAGE_EVENT_PIC_WEIXIN.equals(event)){
						System.out.println("弹出微信相册发图器的事件推送");
					}else if(MessageUtil.REQ_MESSAGE_EVENT_SCANCODE_PUSH.equals(event)){
						System.out.println("扫码推事件的事件推送");
					}else if(MessageUtil.REQ_MESSAGE_EVENT_SCANCODE_WAITMSG.equals(event)){
						System.out.println("扫码推事件且弹出“消息接收中”提示框的事件推送");
					}else if(MessageUtil.REQ_MESSAGE_EVENT_LOCATION_SELECT.equals(event)){
						System.out.println("地理位置事件推送");
					}
				}
			}
			
			pw.close();
			pw = null;
		}
		
		return null;
	}

}
